package com.example.controller.before;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.entity.User;
import com.example.entity.vo.RankVo;
import com.example.service.UserService;
import com.example.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "小程序用户管理") //swagger的注释
@CrossOrigin
@RequestMapping("/userBefore")
@RestController
public class UserControllerBefore {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "获取单个用户信息")
    @GetMapping("getUser/{userId}")
    public R getUser(@PathVariable String userId) {
        User user = userService.getById(userId);
        return R.ok().data("user", user);
    }

    @ApiOperation(value = "修改用户")
    @PostMapping("updateUser")
    public R updateUser(@RequestBody User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", user.getUserId());
        boolean b = userService.update(user, wrapper);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "用户积分排名")
    @GetMapping("creditUser")
    public R creditUser() {
        List<RankVo> list = userService.getRank();
        return R.ok().data("list", list);
    }

    @ApiOperation(value = "用户当前排名")
    @GetMapping("creditUser/{userId}")
    public R creditOneUser(@PathVariable String userId) {
        RankVo one = userService.getRank(userId);
        return R.ok().data("user", one);
    }
}
